package com.cloudspace.yunloan.ui.refresh;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by xuqi on 16/12/14.
 */

public class DividerGridItemDecoration extends RecyclerView.ItemDecoration {

    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

    private Drawable mDivider;
    // 分割线颜色

    public DividerGridItemDecoration(Context context) {
        final TypedArray typedArray = context.obtainStyledAttributes(ATTRS);
        mDivider = typedArray.getDrawable(0);
        typedArray.recycle();
    }


    // 在 onDraw 为 divider 设置绘制范围，并绘制到 canvas 上，
    // 而这个绘制范围可以超出在 getItemOffsets 中设置的范围，
    // 但由于 decoration 是绘制在 child view 的底下，所以并不可见，但是会存在 overdraw
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        drawVertical(c, parent);
        drawHorizontal(c ,parent);
    }

    // 获取列数
    private int getSpanCount(RecyclerView parent) {
        // 列数
        int spanCount = -1;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();

        if (layoutManager instanceof GridLayoutManager) {
            spanCount = (((GridLayoutManager) layoutManager).getSpanCount());
        }
        return spanCount;
    }

    public void getItemOffsets(Rect outRect, View v, RecyclerView parent, RecyclerView.State state) {
        // Return the adapter position that the given child view corresponds to.
        int itemPosition = parent.getChildAdapterPosition(v);
        int spanCount = getSpanCount(parent);
        int childCount = parent.getAdapter().getItemCount();
        // 如果是最后一行，不需要绘制底部
        if (isLastRaw(parent, itemPosition, spanCount, childCount)) {
            // 绘制最后一行左列
            if (isFirstColum(parent, itemPosition, spanCount, childCount)) {
                outRect.set(0, 0, mDivider.getIntrinsicWidth() * 3, 0);
            }
            // 绘制最后一行的右列
            else if (isLastColum(parent, itemPosition, spanCount, childCount)) {
                outRect.set(mDivider.getIntrinsicWidth() * 3, 0, 0, 0);
            }
            // 假如不止两列，则中间列执行这部分，需要绘制left , right , bottom
            else {
                outRect.set(mDivider.getIntrinsicWidth() * 3, 0, mDivider.getIntrinsicWidth() * 3, mDivider.getIntrinsicHeight() * 6);
            }

        } else if (itemPosition == 0) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()*6);
        } else if (itemPosition == 1) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight() * 6);
        }

        // 绘制左列 , 需要绘制 right , bottom
        else if (isFirstColum(parent, itemPosition, spanCount, childCount)) {
            outRect.set(0, 0, mDivider.getIntrinsicWidth() * 3, mDivider.getIntrinsicHeight() * 6);
        }
        // 绘制右列,需要绘制 left , bottom
        else if (isLastColum(parent, itemPosition, spanCount, childCount)) {
            outRect.set(mDivider.getIntrinsicWidth() * 3, 0, 0, mDivider.getIntrinsicHeight() * 6);
        }

        // 假如不是两列，则中间列执行这部分，需要绘制left , right , bottom
        else {
            outRect.set(mDivider.getIntrinsicWidth() * 3, 0, mDivider.getIntrinsicWidth() * 3, mDivider.getIntrinsicHeight() * 6);
        }

    }


    private void drawVertical(Canvas c, RecyclerView parent) {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
//            if (i % 2 == 1) {
//                final View child = parent.getChildAt(i);
//                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
//                final int top = child.getTop() - params.topMargin;
//                final int bottom = child.getBottom() + params.bottomMargin;
//                final int left = child.getRight() + params.rightMargin;
//                final int right = left + mDivider.getIntrinsicWidth() * 3;
//                mDivider.setBounds(left, top, right, bottom);
//                mDivider.draw(c);
//            } else {

            //  先绘制左侧的分割线
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int top = child.getTop() - params.topMargin;
            int bottom = child.getBottom() + params.bottomMargin;
            int left = child.getLeft() - mDivider.getIntrinsicWidth() * 3;
            int right = child.getLeft();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
            // 绘制右侧分割线
            top = child.getTop() - params.topMargin;
            bottom = child.getBottom() + params.bottomMargin;
            left = child.getRight() + params.rightMargin;
            right = left + mDivider.getIntrinsicWidth() * 3;
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }


    private void drawHorizontal(Canvas c, RecyclerView parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getLeft() - params.leftMargin;
            final int right = child.getRight() + params.rightMargin + mDivider.getIntrinsicWidth()*6;
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight()*6;
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    // 如果是最后一列
    private boolean isLastColum(RecyclerView parent, int position, int spanCount, int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            // 如果是最后一列，则不需要绘制右边
            if ((position + 1) % spanCount == 0) {
                return true;
            }
        }
        return false;
    }

    // 如果是第一列
    private boolean isFirstColum(RecyclerView parent, int position, int spanCount, int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            // position从（0,1,2）2开始
            if (position > 1) {
                if (position % spanCount == 0) {
                    return true;
                }
            }
        }
        return false;
    }

    // 如果是最后一行
    private boolean isLastRaw(RecyclerView parent, int position, int spanCount, int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            if (position + spanCount - 1 == childCount || position + spanCount == childCount)
                return true;
        }
        return false;
    }

}
